package com.jic.manager.controller.manage.conf;

import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.entity.CustomerServiceManagement;
import com.jic.conf.request.CustomerServiceManagementRequest;
import com.jic.conf.response.CustomerServiceManagementResponse;
import com.jic.manager.service.CustomerServiceManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/customerServiceManagement")
@Api(value = "客服管理", description = "客服管理", tags = "客服管理")
@Slf4j
public class CustomerServiceManagementController {
    @Autowired
    private CustomerServiceManagementService customerServiceManagementService;
    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取客服列表信息")
    @ResponseBody
    @RequestMapping(value = "/queryPage")
    public RestResult<PageResult<CustomerServiceManagementResponse>> queryPage(@RequestBody CustomerServiceManagementRequest form) {
        PageResult<CustomerServiceManagement> result = customerServiceManagementService.queryPage(form);
        PageResult pageResult = new PageResult<>();
        pageResult.setRows(result.getRows());
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }


    /*
     *  客服新增/修改
     * */
    @ResponseBody
    @RequestMapping(value = "/editCustomer")
    public RestResult<Boolean> insertCustomer(@RequestBody CustomerServiceManagementRequest customerServiceManagementRequest) {
        boolean result;
        try {
            //修改
            if (customerServiceManagementRequest.getId() !=null) {
                long flag=customerServiceManagementService.update(customerServiceManagementRequest);
                result=flag>0?true:false;
            } else {//新增
                long flag=customerServiceManagementService.insert(customerServiceManagementRequest);
                result=flag>0?true:false;
            }
        } catch (Exception e) {
            log.error("CustomerServiceManagementController==>insertCustomer,error",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增异常");
        }
        return RestResult.success(result);


    }

    /*
     *  删除客服
     *
     * */
    @ResponseBody
    @PostMapping(value = "/deleteCustomerService")
    public RestResult deleteCustomer(@RequestBody CustomerServiceManagementRequest id) {
        RestResult restResult = new RestResult<>();
        try {
            long flag = customerServiceManagementService.delete(id);
            if (flag == 1) {
                restResult = RestResult.success(restResult);
            }
        } catch (Exception e) {
            log.error("删除客服异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除客服异常");
        }
        return restResult;

    }

}
